package com.xexun.gpstracker.vo;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by HH
 * Date: 2016/4/5 0005
 * Time: 下午 3:12
 */
public final class GTLatLng implements Parcelable, Cloneable {
    public static final LatLngCreator CREATOR = new LatLngCreator();
    public final double latitude;
    public final double longitude;
    private static DecimalFormat fmt=new DecimalFormat("0.000000", new DecimalFormatSymbols(Locale.US));

    public GTLatLng(double latitude, double longitude) {
        this(latitude, longitude, true);
    }

    public GTLatLng(double latitude, double longitude, boolean check) {
        if(check) {
            if(-180.0D <= longitude && longitude < 180.0D) {
                this.longitude = parse(longitude);
            } else {
                this.longitude = parse(((longitude - 180.0D) % 360.0D + 360.0D) % 360.0D - 180.0D);
            }
            this.latitude = parse(Math.max(-90.0D, Math.min(90.0D, latitude)));
        } else {
            this.latitude = latitude;
            this.longitude = longitude;
        }

    }

    private static double parse(double val) {
        return Double.parseDouble(fmt.format(val));
    }

    public GTLatLng clone() {
        try {
            super.clone();
        } catch (CloneNotSupportedException e) {
            //
        }
        return new GTLatLng(this.latitude, this.longitude);
    }

    public int hashCode() {
        byte var1 = 31;
        byte var2 = 1;
        long var3 = Double.doubleToLongBits(this.latitude);
        int var5 = var1 * var2 + (int)(var3 ^ var3 >>> 32);
        var3 = Double.doubleToLongBits(this.longitude);
        var5 = var1 * var5 + (int)(var3 ^ var3 >>> 32);
        return var5;
    }

    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        } else if(!(obj instanceof GTLatLng)) {
            return false;
        } else {
            GTLatLng var2 = (GTLatLng)obj;
            return Double.doubleToLongBits(this.latitude) == Double.doubleToLongBits(var2.latitude) && Double.doubleToLongBits(this.longitude) == Double.doubleToLongBits(var2.longitude);
        }
    }

    public String toString() {
        return "lat/lng: (" + this.latitude + "," + this.longitude + ")";
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeDouble(this.longitude);
        parcel.writeDouble(this.latitude);
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }
}

